﻿#region License
/*
Copyright (c) <2012> <Nercoding>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Original Code by Gastric of Dawnsong
 */
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace AC2_MultiClient.Mouse
{
    public class PerformMouseFunction
    {
        #region MouseActions
        // Pinvokes
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);

        /// <summary>
        /// Returns a handle to a process using classname from spy++ tool
        /// and the name of the Window
        /// </summary>
        /// <param name="lpClassName">Class name from spy++ tool</param>
        /// <param name="lpWindowName">Window name of the application</param>
        /// <returns></returns>
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);



        [DllImport("user32.dll")]
        public static extern int SetWindowText(IntPtr hWnd, string text);

        /// <summary>
        /// Returns the size of a processes Window
        /// </summary>
        /// <param name="hWnd">the Process.WindowHandle</param>
        /// <returns></returns>
        public static Size GetControlSize(IntPtr hWnd)
        {
            Rectangle pRect;
            Size cSize = new Size();
            // get coordinates relative to window
            GetWindowRect(hWnd, out pRect);

            cSize.Width = pRect.Right - pRect.Left;
            cSize.Height = pRect.Bottom - pRect.Top;

            return cSize;
        }
        /// <summary>
        /// Performs a mouse action
        /// Move doesnt seem to work correctly
        /// </summary>
        /// <param name="action">our Mouse_Action enum</param>
        /// <param name="PosX">X screen position</param>
        /// <param name="PosY">Y screen Position</param>
        public static void DoMouseAction(Mouse_Action action, int PosX, int PosY)
        {
            // Left Click
            if (action == Mouse_Action.LeftDown)
            {
                mouse_event((int)Mouse_Action.LeftDown, PosX, PosY, 0, 0);
                mouse_event((int)Mouse_Action.LeftUp, PosX, PosY, 0, 0);
            }
            // Right Click
            if (action == Mouse_Action.RightDown)
            {
                mouse_event((int)Mouse_Action.RightDown, PosX, PosY, 0, 0);
                mouse_event((int)Mouse_Action.RightUp, PosX, PosY, 0, 0);
            }
            // Middle Click
            if (action == Mouse_Action.MiddleDown)
            {
                mouse_event((int)Mouse_Action.MiddleDown, PosX, PosY, 0, 0);
                mouse_event((int)Mouse_Action.MiddleUp, PosX, PosY, 0, 0);
            }
            // Mouse Move, which doesnt seem to work right
            if (action == Mouse_Action.Move)
            {
                mouse_event((int)Mouse_Action.Absolute | (int)Mouse_Action.Move,
                    PosX, PosY, 0, 0);
            }
        }
        /// <summary>
        /// Moves the mouse cursor to the Point
        /// </summary>
        /// <param name="p">The point to move too</param>
        public static void PointMouseCursorAt(Point p)
        {
            try
            {
                Cursor.Position = p;
            }
            catch { }
        }
        public void Keys(string key)
        {
        }
        #endregion
    }
}

